package com.example.shoppingmall.cotroller;

import com.example.shoppingmall.config.MyLog;
import com.example.shoppingmall.pojo.Conditioned;
import com.example.shoppingmall.pojo.Consequence;
import com.example.shoppingmall.pojo.Coupon;
import com.example.shoppingmall.service.CouponService;
import com.example.shoppingmall.tool.PoiUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/coupon")
public class CouponController {

    @Resource
    CouponService couponService;

    /**
     * 分页，查询，回显
     * @param ono
     * @param dangIng
     * @param name
     * @param statusa
     * @param startTime
     * @param endTime
     * @param model
     * @return
     */
    @RequestMapping("/couponList")
    public String getAll(
            @RequestParam(value = "ono", required = false) String ono,
            @RequestParam(value = "dang", required = false) String dangIng,
            @RequestParam(value = "couponName", required = false) String name,
            @RequestParam(value = "status", required = false) String statusa,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            Model model) {
        int dang = 1;
        if (dangIng != null & dangIng != "") {
            dang = Integer.parseInt(dangIng);
        }
        if (statusa == null | statusa == "") {
            statusa = "0";
        }
        if (name == null) {
            name = "";
        }
        if (startTime == null) {
            startTime = "";
        }
        if (endTime == null) {
            endTime = "";
        }
        if (ono == null | ono == "") {
            ono = "1";
        }
        int size = 6;


        int count = couponService.count(Integer.parseInt(ono), name, Integer.parseInt(statusa), startTime, endTime);
        int zong = count % size == 0 ? count / size : count / size + 1;
        model.addAttribute("coupon", couponService.getAll(Integer.parseInt(ono), name, Integer.parseInt(statusa), startTime, endTime, dang, size));
        model.addAttribute("dang", dang);
        model.addAttribute("zong", zong);

        model.addAttribute("name", name);
        model.addAttribute("status", statusa);
        model.addAttribute("startTime", startTime);
        model.addAttribute("endTime", endTime);

        return "/coupon/coupon";
    }


    /**
     * 跳到新增页面
     * @param model
     * @return
     */
    @RequestMapping("/toAddCoupon")
    public String toAddCoupon(Model model) {

        List<Conditioned> list = couponService.getAllConditioned();
        List<Consequence> list1 = couponService.getAllConsequence();
        model.addAttribute("list", list);
        model.addAttribute("list1", list1);

        return "/coupon/addCoupon";
    }

    /**
     * 新增优惠券
     * @param coupon
     * @return
     */
    @MyLog(value = "促销管理->优惠卷列表->新增优惠券")
    @RequestMapping("/addCoupon")
    public String addCoupon(Coupon coupon) {
        int num = couponService.add(coupon);
        if (num > 0) {
            return "redirect:/coupon/couponList";
        }
        return "/coupon/addCoupon";
    }

    /**
     * 跳到修改页面
     * @param id
     * @param model
     * @return
     */
    @MyLog(value = "促销管理->优惠卷列表->修改详情")
    @RequestMapping("/toUpdateCoupon")
    public String toUpdateCoupon(int id, Model model) {

        List<Conditioned> list = couponService.getAllConditioned();
        List<Consequence> list1 = couponService.getAllConsequence();
        model.addAttribute("list", list);
        model.addAttribute("list1", list1);
        model.addAttribute("couponById", couponService.getById(id));

        return "/coupon/updateCoupon";
    }

    /**
     * 修改优惠券
     * @param coupon
     * @return
     */
    @MyLog(value = "促销管理->优惠卷列表->修改优惠券")
    @RequestMapping("/updateCoupon")
    public String updateCoupon(Coupon coupon) {
        int num = couponService.update(coupon);
        if (num > 0) {
            return "redirect:/coupon/couponList";
        }
        return "/coupon/updateCoupon";
    }

    /**
     * 删除，主键所有先删除外键
     * @param id
     * @return
     */
    @MyLog(value = "促销管理->优惠卷列表->删除优惠券")
    @RequestMapping("/del")
    public String del(int id) {
        int num = couponService.del(id);
        int result=couponService.delTicketCouponId(id);
        if (num > 0 && result>0) {
            return "redirect:/coupon/couponList";
        }
        return "/coupon/coupon";
    }

    /**
     * 根据id导出当前优惠券信息
     *
     * @param id
     * @return
     */
    @MyLog(value = "促销管理->优惠卷列表->导出")
    @RequestMapping(value = "/exportCoupon", method = RequestMethod.GET)
    public ResponseEntity<byte[]> exportCoupon(int id) {
        return PoiUtils.exportcoupon2Excel(couponService.getById(id));
    }

    /**
     * 跳到先对应的券码列表，并且查询，分页，回显
     * @param id
     * @param dangIng
     * @param couponNameT
     * @param employ
     * @param generatedTime
     * @param model
     * @return
     */
    @MyLog(value = "促销管理->优惠卷列表->跳转卷码列表")
    @RequestMapping("/toTicketList")
    public String ticketList(
            @RequestParam(value = "id", required = false) String id,
            @RequestParam(value = "dang", required = false) String dangIng,
            @RequestParam(value = "couponNameT", required = false) String couponNameT,
            @RequestParam(value = "employ", required = false) String employ,
            @RequestParam(value = "generatedTime", required = false) String generatedTime,
            Model model) {
            int dang = 1;
            if (dangIng != null & dangIng != "") {
                dang = Integer.parseInt(dangIng);
            }
            if (employ == null | employ == "") {
                employ = "0";
            }
            if (id == null | id == "") {
                id = "0";
            }
            if (couponNameT == null) {
                couponNameT = "";
            }
            if (generatedTime == null) {
                generatedTime = "";
            }

            int size = 3;

            int count = couponService.countByIdTick(couponNameT,Integer.parseInt(employ),generatedTime,Integer.valueOf(id));
            int zong = count % size == 0 ? count / size : count / size + 1;
            model.addAttribute("ticketList", couponService.getByIdTick(couponNameT,Integer.parseInt(employ),generatedTime,Integer.parseInt(id),dang,size));
            model.addAttribute("dang", dang);
            model.addAttribute("zong", zong);

            model.addAttribute("id",id);
            model.addAttribute("couponNameT", couponNameT);
            model.addAttribute("employ", employ);
            model.addAttribute("generatedTime", generatedTime);
            model.addAttribute("count", count);


        return "/coupon/ticketList";
    }

    /**
     * 删除券码列表 外键
     * @param id
     * @return
     */
    @MyLog(value = "促销管理->优惠卷列表->删除卷码")
    @RequestMapping("/delTicket")
    public String delTicket(int id){
        int num=couponService.delTicket(id);
        if (num>0){
            return "redirect:/coupon/toTicketList";
        }
        return "/coupon/ticketList";
    }
}
